package carson.web.mvc.demo.controllers;

import carson.web.mvc.*;
import carson.web.mvc.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Demo controller.
 */
@Controller
public class DemoController extends CarsonActionController {
    /**
     * Carson-Web-MVC演示页面
     *
     * @return the model and view
     */
    public ModelAndView demoView() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("title", "Carson-Web-MVC演示");
        return mv;
    }

    /**
     * 打开页面案例
     *
     * @return the model and view
     */
    public ModelAndView modelAndViewDemo() {
        ModelAndView mv = new ModelAndView("demo/modelAndViewDemo");
        return mv;
    }

    /**
     * 返回字符串案例
     *
     * @return the content result
     */
    public ContentResult contentResultDemo() {
        return Content("<p style='color:red'>我是后台返回的ContentResult</p>");
    }

    /**
     * 返回JSON案例
     *
     * @return the json result
     */
    public JsonResult jsonResultDemo() {
        User user=new User();
        user.setUsername(getPara("username"));
        user.setAge(Integer.parseInt(getPara("age")));
        user.setModifyDate(new Date());
        return Json(user);
    }

    /**
     * 返回JavaScript代码案例
     *
     * @return the java script result
     */
    public JavaScriptResult javaScriptResultDemo() {
        return JavaScript("alert('我是后台返回的javaScript');");
    }

    /**
     * Ajax返回页面案例
     *
     * @return the model and view
     */
    public ModelAndView partialViewDemo() {
        ModelAndView mv = new ModelAndView();
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowStr = simpleDateFormat.format(now);
        mv.addObject("now", nowStr);
        return mv;
    }

    /**
     * 页面重定向案例
     *
     * @return the redirect result
     */
    public RedirectResult redirectResultDemo() {
        return Redirect("http://www.baidu.com");
    }

    /**
     * 文件下载案例（文件路径）
     *
     * @return the file path result
     */
    public FilePathResult filePathResultDemo() {
        String rootPath = getRequest().getServletContext().getRealPath("/");
        String filePath = rootPath + File.separator + "attachment" + File.separator + "testfile.zip";
        return File(filePath, "application/zip");
    }

    /**
     * 文件下载案例（文件流）
     *
     * @return the file stream result
     * @throws FileNotFoundException the file not found exception
     */
    public FileStreamResult fileStreamResultDemo() throws FileNotFoundException {
        String rootPath = getRequest().getServletContext().getRealPath("/");
        String filePath = rootPath + File.separator + "attachment" + File.separator + "testfile.zip";
        FileInputStream fileInputStream = new FileInputStream(filePath);
        return File(fileInputStream, "application/zip", "testfile.zip");
    }

    /**
     * 文件下载案例（文件内容byte数组）
     *
     * @return the file content result
     * @throws IOException the io exception
     */
    public FileContentResult fileContentResultDemo() throws IOException {
        String rootPath = getRequest().getServletContext().getRealPath("/");
        String filePath = rootPath + File.separator + "attachment" + File.separator + "testfile.zip";
        File file = new File(filePath);
        long len = file.length();
        byte[] bytes = new byte[(int) len];
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        int r = bufferedInputStream.read(bytes);
        bufferedInputStream.close();
        return File(bytes, "application/zip", "testfile.zip");
    }

    public ModelAndView urlParaDemo() {
        String urlPara0 = getPara(0);//value:a
        String urlPara1 = getPara(1);//value:123
        ModelAndView mv = new ModelAndView("demo/urlParaDemo");
        mv.addObject("urlPara0", urlPara0);
        mv.addObject("urlPara1", urlPara1);
        return mv;
    }

    public ModelAndView formDataDemo() {
        User user = getModel(User.class);
        User user2 = getModel(User.class, "user2");
        ModelAndView mv = new ModelAndView("demo/formDataDemo");
        mv.addObject("user", user);
        mv.addObject("user2", user2);
        return mv;
    }

}
